package top.roadvast.pdshopping.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.stereotype.Service;
import top.roadvast.pdshopping.bean.PdLog;
import top.roadvast.pdshopping.service.LogService;

import javax.annotation.Resource;

/**
 * @Author 郭泽鹏
 * @Date 2021/12/29 9:51
 * @Version 1.0
 * @Description 日志类业务层
 */
@Service
@Slf4j
public class LogServiceImpl implements LogService {
    @Resource
    AmqpTemplate amqpTemplate;

    private static String[] levels;

    private static String[] users;

    static {
        levels = new String[]{"", "info", "warning", "error"};
        users = new String[]{"Lucy", "RoadVast", "gjn998"};
    }

    @Override
    public String saveLogByLevel(PdLog pdLog) {
        log.info("level = " + levels[pdLog.getLevel()]);
        //交换机，routingKey，message
        amqpTemplate.convertAndSend("pd_log_direct_exchange", levels[pdLog.getLevel()], pdLog);
        return "OK";
    }

    @Override
    public String saveAllLog(PdLog pdLog) {
        //交换机，routingKey：广播模式不用传，message
        amqpTemplate.convertAndSend("pd_log_fanout_exchange", "", pdLog);
        return "OK";
    }

    @Override
    public String saveLogByLevelAndUser(PdLog pdLog) {
        System.out.println("level = " + levels[pdLog.getLevel()] +
                ", users = " + users[Math.toIntExact(pdLog.getUserId())]);
        //交换机，routingKey：主题模式[xx.xxx]，message
        amqpTemplate.convertAndSend("pd_log_topic_exchange",
                levels[pdLog.getLevel()] + "." + users[Math.toIntExact(pdLog.getUserId())],
                pdLog);
        return "OK";
    }
}
